﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

public class scene_001 : MonoBehaviour
{
    [CSharpCallLua]
    public delegate int Add(int a, int b, out int resa,out int resb);

    // Use this for initialization
    void Start()
    {
        LuaEnv luaEnv = new LuaEnv();
        luaEnv.DoString("require 'scene_001'");

        Action act1 = luaEnv.Global.Get<Action>("csharpCallLua");
        act1();
        act1 = null;

        //1、映射到Delegate
        int res1;
        int res2;
        Add add = luaEnv.Global.Get<Add>("add");
        int res = add(1, 4, out res1, out res2);
        Debug.Log(res + " " + res1 + " " + res2);
        add = null;


        //2,通过LuaFunction调用
        LuaFunction func = luaEnv.Global.Get<LuaFunction>("add");
        object[] os = func.Call(1, 2);
        foreach (object o in os)
        {
            print(o);
        }

        //3，事件Action<int,int>
        Action<int, int> actAdd = luaEnv.Global.Get<Action<int, int>>("add");
        actAdd(11, 22);
        actAdd = null;

        //3,通过interface
        IPerson p = luaEnv.Global.Get<IPerson>("person");
        print(p.name + "-" + p.age);
        p.name = "modify name";
        luaEnv.DoString("print(person.name)");
        p.eat(12, 34);//  p.eat(p,12,34);

        luaEnv.Dispose();
    }

    [CSharpCallLua]
    public interface IPerson
    {
        string name { get; set; }
        int age { get; set; }
        void eat(int a, int b);
    }
}
